<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>弹幕飘萍</title>

    <style>
        *{
            padding: 0;
            margin: 0;
            box-sizing: border-box;
        }

        .container{
            width: 1200px;
            margin: 0  auto;
        }

        .video{
            width: 100%;
            height: 800px;
            background: rgba(0,0,0,0.2);
            position: relative;
            overflow: hidden;
        }


        #input{
            width: 300px;
            height: 50px;
            font-size: 20px;
            padding: 12px;
        }

        #send{
            height: 50px;
            font-size: 20px;
            padding: 12px;
        }

        .bullet{
            font-size: 14px;
            color: red;
            border: 1px solid red;
            position: absolute;
            top: 50px;
            left: 1200px;
            white-space: nowrap;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="video" id="bullet-container">
        </div>
        <div>
            <input id="input" type="text" placeholder="输入弹幕">
            <button id="send">发送</button>
        </div>
    </div>

    <script>
        let bulletList = [];
        let bulletWrap = document.getElementById("bullet-container");
        let input = document.getElementById("input");
        let btn = document.getElementById("send");

        // 计时器的ID
        let timer = null;

        btn.addEventListener("click", btnClick);

        // 按钮点击事件
        function btnClick(event){
            let inputValue = input.value.trim();
            // console.log(inputValue);
            if(inputValue){
                let bulletDiv = document.createElement("div");
                bulletDiv.classList.add("bullet");
                bulletDiv.style.left = "1200px";
                bulletDiv.style.top = getRandom(400)+"px";
                bulletDiv.innerText = inputValue;

                bulletList.push({
                    speed:getRandom(5)+3,
                    left:1200,
                    dom:bulletDiv
                });

                bulletWrap.appendChild(bulletDiv);

                if(!timer){
                    timer = setInterval(draw, 17);
                }
            }

            input.value="";
        }

        // 绘制每一帧画面
        function draw(){
            for(let i=0; i<bulletList.length; i++){
                let left = bulletList[i].left;
                left -= bulletList[i].speed;
                bulletList[i].dom.style.left = left+"px";
                bulletList[i].left = left;
 
                let width = bulletList[i].dom.getBoundingClientRect().width;
                if(bulletList[i].left < -width){
                    bulletList[i].dom.remove();
                    bulletList.splice(i,1);
                    i--;
                }
            }

            if(bulletList.length<1){
                clearInterval(timer);
                timer=null;
            }

            console.log("绘制");
        }

        // 获取1~n的随机数
        function getRandom(n){
            let seed = Math.random();
            return Math.floor(seed*n+1);
        }
    </script>
</body>
</html>